<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'basic';
$tabs=array(
	array('op'=>'basic','title'=>'基础设置'),
	array('op'=>'list','title'=>'商品送积分'),
	array('op'=>'edit','title'=>'添加商品'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='basic'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('buy_credit',$post);
		exi('设置成功');
	}
	$item=$this->plugin_setting('buy_credit');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'积分优惠',
		'edit'=>array(
			array('type'=>'switch','name'=>'open','title'=>'是否启用','help'=>''),
			array('type'=>'checkbox','name'=>'paytypes','title'=>'支付场景','options'=>'paytypes','help'=>'设置用户哪些支付方式付款时赠送积分'),
			array('type'=>'radio','name'=>'type','title'=>'送积分方式','options'=>'types','help'=>'如果设置为根据商品送积分，请点击上方添加商品并设置赠送数额'),
			array('type'=>'radio','name'=>'time','title'=>'赠送时机','options'=>'times','help'=>''),
			array('type'=>'array_string_much','name'=>'options','title'=>'积分赠送机制','show'=>array('type'=>1),'fields'=>array(
				array('title'=>'购物'),
				array('name'=>'start'),
				array('title'=>'元至'),
				array('name'=>'end'),
				array('title'=>'元|每消费1元赠送'),
				array('name'=>'credit'),
				array('title'=>'积分'),
			)),
		)
	);
	$return['paytypes']=array('1'=>'微信支付','2'=>'余额支付','3'=>'支付宝支付');
	$return['times']=array(
		'create'=>'创建订单时',
		'pay'=>'支付完毕时',
		'complete'=>'订单完成时',
	);
	$return['types']=array(
		'1'=>'根据订单金额赠送积分',
		'2'=>'根据商品赠送积分',
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
	);
	$list=$this->get_list('goods_credit',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['goods']=$this->model('goods')->goods_detail($item['goods_id'],array('title','image'));
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'goods.title','title'=>'商品名称','type'=>'string'),
			array('name'=>'goods.image','title'=>'商品图片','type'=>'image'),
			array('name'=>'credit','title'=>'购买赠送积分','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>18,'action'=>'jy_weishop/goods.get_goods/','name'=>'goods_id','title'=>'选择商品'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'credit'=>intval($post['credit']),
		);
		$id=intval($post['id']);
		if($id){
			$this->pdo_update('goods_credit',$data,array('id'=>$id));
		}else{
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'createtime'=>TIMESTAMP,
			);
			$insert=array_merge($insert,$data);
			$this->pdo_insert('goods_credit',$insert);
		}
		exi('设置成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->pdo_get('goods_credit',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'choose','name'=>'goods_id','title'=>'选择商品','action'=>'jy_weishop/goods.get_goods/'),
			array('type'=>'string','name'=>'credit','title'=>'赠送积分'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$params=array(
		'tablename'=>$this->tablename('goods_credit'),
	);
	model('core/php_ctrl')->del($params);
}